import { _decorator, Component, Node, Sprite, SpriteFrame } from 'cc';
import { PinAction } from './PinAction';
import { EmptyHoleAction } from './EmptyHoleAction';
const { ccclass, property } = _decorator;

@ccclass('LayerEmptyAction')
export class LayerEmptyAction extends Component {

    
    @property(Sprite)
    target_spr:Sprite = null;

    @property([SpriteFrame])
    unlockSprs:SpriteFrame[] = [];

    cur_unlock_lv = 0;

    onLoad() {
        // target_spr node 需要在 layer_root 层级的下面，但是动画需要在层级上面，已写好，加个 target_spr 只做显示
        this.target_spr.node.setPosition(this.node.getPosition())
    }

    start() {

    }


    public get_pin_by_color(color_id: number, pin_arr: PinAction[]) {
        this.node.children.forEach(empty_hole => {
            empty_hole.getComponent(EmptyHoleAction)?.get_pin_arr_by_color_id(color_id, pin_arr);
        });
    }

    public get_pin_arr(arr: PinAction[] = null): PinAction[] {
        // let arr:PinAction[] = [];
        if (!arr) {
            arr = [];
        }
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            let empty_hole = this.node.children[i];
            let empty_hole_action = empty_hole.getComponent(EmptyHoleAction);
            if (empty_hole_action) {
                empty_hole_action.get_pin_arr(arr);
            }
        }
        return arr;
    }


    public put_pin(pin: PinAction) {
        //console.log("children：", this.node.children.length)

        //判断下面孔的是否满了
        for (let i = 0; i < this.node.children.length; i++) {
            let empty_hole_action = this.node.children[i].getComponent(EmptyHoleAction);

            if (empty_hole_action?.can_able_put()) {
                //console.log("can_able_put")
                if (empty_hole_action?.put_pin(pin)) {
                    //放入成功，结束
                    //console.log("111")
                    break;
                }
            }
        }
    }

    //true = 满了 ， 游戏结束 返回
    public is_pin_full(): boolean {
        let ret = true;
        for (let i = 0; i < this.node.children.length; i++) {
            let empty_hole = this.node.children[i];
            let empty_hole_action = empty_hole.getComponent(EmptyHoleAction);
            if (!empty_hole_action) {
                continue;
            }
            if (empty_hole_action?.can_able_put()) {
                ret = false;
                break;
            }
        }
        return ret;
    }

    //check 获取上锁的个数
    get_unlock_num(): number {
        let num = 0;
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            // console.log("unlock_empty_hole  i:",i);
            let empty_hole = this.node.children[i];
            let empty_hole_action = empty_hole.getComponent(EmptyHoleAction);
            if (empty_hole_action?.isLocked) {
                num++
            }
        }
        return num;
    }


    // public get_pin_arr(arr:PinAction[] = null): PinAction[] {

    unlock_empty_hole() {
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            // console.log("unlock_empty_hole  i:",i);
            let empty_hole = this.node.children[i];
            let empty_hole_action = empty_hole.getComponent(EmptyHoleAction);
            if (empty_hole_action?.isLocked) {
                empty_hole_action.unlock_hole(null);
                break;
            }
        }
    }
    
    /** 增加孔 */
    add_empty_hole() {
        let _length = this.node.children.length - 1;
        if(!this.node.children[_length].getComponent(EmptyHoleAction)?.isLocked){
            console.log("已全部解锁")
            return;
        }

        this.cur_unlock_lv++;
        this.target_spr.spriteFrame = this.unlockSprs[this.cur_unlock_lv];
        let _unlock = false;

        for (let i = 0; i < this.node.children.length; i++) {
            let empty_hole = this.node.children[i];
            let empty_hole_action = empty_hole.getComponent(EmptyHoleAction);
            if (empty_hole_action?.isLocked && !_unlock) {
                empty_hole_action.unlock_hole(null);
                _unlock = true;
            }

            let _pos = empty_hole.getPosition();
            _pos.x -= 26;
            empty_hole.setPosition(_pos);
            
        }
    }



    public init_empty() {
        this.node.children.forEach(empty_hole => {
            empty_hole.getComponent(EmptyHoleAction)?.init_empty_hole();
        });
    }
}

